/*
 Copyright (c) Whitemice Consulting, 2007, 2008, 2009
 Adam Tauno Williams <awilliam@whitemice.org>
 License: MIT/X11

 Permission is hereby granted, free of charge, to any person obtaining a copy
 of this software and associated documentation files (the "Software"), to deal
 in the Software without restriction, including without limitation the rights
 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 copies of the Software, and to permit persons to whom the Software is
 furnished to do so, subject to the following conditions:

 The above copyright notice and this permission notice shall be included in
 all copies or substantial portions of the Software.
 
 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 THE SOFTWARE.
*/
using System;
using System.Collections;
using Gtk;
using Whitemice.ZOGI;

namespace Whitemice.ZOGI.GtkWidgets
{	
	public static class ZOGIUtility  
	{		
		public static string TaskStatusDescription(TaskStatus _status)
		{
			switch (_status)
			{
				case TaskStatus.Created: return "Created"; 
				case TaskStatus.Accepted: return "Accepted"; 
				case TaskStatus.Processing: return "Processing"; 
				case TaskStatus.Done: return "Done"; 
				case TaskStatus.Archived: return "Archived"; 
				case TaskStatus.Rejected: return "Rejected"; 
				case TaskStatus.Commented: return "Commented"; 
				case TaskStatus.Divided: return "Divided";;
				case TaskStatus.Reactivated: return "Reactivated"; 
				default: return "N/A";
			}
		}
		
		public static string TaskPriorityDescription(TaskPriority priority)
		{
			return TaskPriorityDescription((uint)priority);
		}
		
		public static string TaskPriorityDescription(uint priority)
		{
			switch (priority)
			{
				case 0: return "Very High";
				case 1: return "High";
				case 2: return "Average";
				case 3: return "Low";
				case 4: return "Very Low";
				default: return "N/A";
			}
		}
		
		public static ArrayList AvailableTaskActions(Task task)
		{
			ArrayList	actions;
			
			actions = new ArrayList();
			// if you can see it, you can comment 
			actions.Add(TaskAction.Comment);
			// if you are the creator, you can archive it 
			if ((task.CreatorId == Server.Connection.UserId) &&
			    (task.Status != TaskStatus.Archived))
				actions.Add(TaskAction.Archive);
		  // it a task is new it can be accepted or rejected 
			if (task.Status == TaskStatus.Created)
			{
				actions.Add(TaskAction.Accept);
				actions.Add(TaskAction.Reject);
			}
			// task executant or creator can mark it as done 
			if (task.Status != TaskStatus.Done)
			{
			  if (task.CreatorId == Server.Connection.UserId)
			  {
			    actions.Add(TaskAction.Done);
			  } else if (task.ExecutorId == Server.Connection.UserId)
			    {
			      actions.Add(TaskAction.Done);
			    } else
			      {
              /*
			        if (ZOGIApplication.Server.UserTeamIds.Contains(task.ExecutantId))
			        {
			          actions.Add(TaskAction.Done);
			        }
			        */
			     }
			} // end if-not-done
			// if the user is the owner of the task and the task is done or
			// archived then the user can reactivate the task 
			if (((task.Status == TaskStatus.Archived) || 
			     (task.Status == TaskStatus.Done)) &&
			    (task.CreatorId == Server.Connection.UserId))
				actions.Add(TaskAction.Activate);
			return actions;
		} // End AvailableTaskActions method
		
		public static Window GetWindow(Widget _widget)
		{
			if (_widget == null) 
				return null;
			if (_widget is Gtk.Window)
				if((_widget as Gtk.Window).Type == Gtk.WindowType.Toplevel)
					return (_widget as Gtk.Window);
			return GetWindow(_widget.Parent);
		} /* End GetWindow */
	} // End class Utility
} // End namespace